/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.ce.entitybeans.query;

import com.bionic.ce.entities.Query;
import com.bionic.ce.entitybeans.AbstractFacade;
import java.sql.Timestamp;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author Jarik
 */
@Stateless
public class QueryFacade extends AbstractFacade<Query> implements QueryFacadeLocal {

    @PersistenceContext(unitName = "Currency_ExchangeEE-PU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public QueryFacade() {
        super(Query.class);
    }

    @Override
    public List<Query> findNotActiveQueries(Integer userId) {
        String queryString = "SELECT q FROM Query q WHERE status<>\'Active\' AND openByUser=" + userId;
        TypedQuery<Query> tpquery = em.createQuery(queryString, Query.class);

        List<Query> querylist = null;
        try {
            querylist = tpquery.getResultList();
        } catch (NoResultException e) {
            e.printStackTrace();
        }
        return querylist;
    }

    @Override
    public List<Query> findByDynamicSelect(Query query) {
        String queryString = "SELECT q FROM Query q";
        int i = 0;
        if (query.getId() != null) {
            if (i == 0) queryString += " WHERE ";
            else if (i > 0) queryString += " AND ";
            queryString += "id=" + query.getId();
            i++;
        }
        if (query.getOpenByUser() != null) {
            if (i == 0) queryString += " WHERE ";
            else if (i > 0) queryString += " AND ";
            queryString += "openByUser=" + query.getOpenByUser().getId();
            i++;
        }
        if (query.getOpenDate() != null) {
            if (i == 0) queryString += " WHERE ";
            else if (i > 0) queryString += " AND ";
            queryString += "openDate=\'" + new Timestamp(query.getOpenDate().getTime()) + "\'";
            i++;
        }
        if (query.getFinishDate() != null) {
            if (i == 0) queryString += " WHERE ";
            else if (i > 0) queryString += " AND ";
            queryString += "finishDate=\'" + new Timestamp(query.getFinishDate().getTime()) + "\'";
            i++;
        }
        if (query.getAmount() != null) {
            if (i == 0) queryString += " WHERE ";
            else if (i > 0) queryString += " AND ";
            queryString += "amount=" + query.getAmount();
            i++;
        }
        if (query.getCurrencyFrom() != null) {
            if (i == 0) queryString += " WHERE ";
            else if (i > 0) queryString += " AND ";
            queryString += "currencyFrom=\'" + query.getCurrencyFrom().getShortName() + "\'";
            i++;
        }
        if (query.getCurrencyTo() != null) {
            if (i == 0) queryString += " WHERE ";
            else if (i > 0) queryString += " AND ";
            queryString += "currencyTo=\'" + query.getCurrencyTo().getShortName() + "\'";
            i++;
        }
        if (query.getRateFrom() != null) {
            if (i == 0) queryString += " WHERE ";
            else if (i > 0) queryString += " AND ";
            queryString += "rateFrom=" + query.getRateFrom();
            i++;
        }
        if (query.getRateTo() != null) {
            if (i == 0) queryString += " WHERE ";
            else if (i > 0) queryString += " AND ";
            queryString += "rateTo=" + query.getRateTo();
            i++;
        }
        if (query.getStatus() != null) {
            if (i == 0) queryString += " WHERE ";
            else if (i > 0) queryString += " AND ";
            queryString += "status=\'" + query.getStatus() + "\'";
            i++;
        }
        if (query.getExecutedByUser() != null) {
            if (i == 0) queryString += " WHERE ";
            else if (i > 0) queryString += " AND ";
            queryString += "executedByUser=" + query.getExecutedByUser().getId();
            i++;
        }
        if (query.getClosedRate() != null) {
            if (i == 0) queryString += " WHERE ";
            else if (i > 0) queryString += " AND ";
            queryString += "closedRate=" + query.getClosedRate();
            i++;
        }
        if (query.getClosedDate() != null) {
            if (i == 0) queryString += " WHERE ";
            else if (i > 0) queryString += " AND ";
            queryString += "closedDate=\'" + new Timestamp(query.getClosedDate().getTime()) + "\'";
            i++;
        }
        TypedQuery<Query> tpquery = em.createQuery(queryString, Query.class);

        List<Query> querylist = null;
        try {
            querylist = tpquery.getResultList();
        } catch (NoResultException e) {
            e.printStackTrace();
        }
        return querylist;
    }
}
